import React, { Component } from 'react'

const { Consumer: MapConsumer, Provider } = React.createContext();

export default class MapProvider extends Component {

  constructor() {
    super();
    this.state = {
      showMap:localStorage.getItem('showMap')===''? true :
      JSON.parse(localStorage.getItem('showMap'))
    }
  }

  changeMap=()=> {
    this.setState((preState) => {
      return {
        showMap: !preState.showMap
      }
    },()=>{
      localStorage.setItem('showMap',this.state.showMap)
    })
  }

  render() {
    return (
      <Provider value={{
        showMap: this.state.showMap,
        changeMap: this.changeMap
      }}>
        {this.props.children}
      </Provider>
    )
  }
}

export {
  MapConsumer,
  MapProvider
}
